//
//  WBTabBarController.swift
//  SinaWeibo
//
//  Created by zhuangqianwei on 16/11/12.
//  Copyright © 2016年 zhuangqianwei. All rights reserved.
//

import UIKit

class WBTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
   
        //自定义tabbar,初始化
        let WBTabBar = WBLayoutView()
        // Cannot assign to property: 'tabBar' is a get-only property
        //只读属性 不能使用直接赋值，可以间接赋值
//        self.tabBar = WBTabBar
        self.setValue(WBTabBar, forKey: "tabBar")
        
        //监听点击事件
        WBTabBar.composeClosure = {[weak self] in
            
            
           print(self!)
            
            let composeView = WBComposeView()
            composeView.show(fromVC: self)
//            UIApplication.shared.keyWindow?.addSubview(composeView)
        }
        
//        self.view.addSubview(WBTabBar)
        
        
        
        addChildViewControllers()
    }

  
    //添加子视图控制器
    //private 不希望外界能够访问到该方法, 只能在本类中访问
    private func addChildViewControllers() {
        addChildViewController(vc: WBHomeController(), title: "首页", imageName: "tabbar_home",index:0)
        addChildViewController(vc: WBMessageController(), title: "消息", imageName: "tabbar_message_center",index:1)
        addChildViewController(vc: WBDiscoverController(), title: "发现", imageName: "tabbar_discover",index:2)
        addChildViewController(vc: WBProfileController(), title: "我", imageName: "tabbar_profile",index:3)
    }
    
    //MARK: 添加子视图控制器
    private func addChildViewController(vc: UIViewController, title: String, imageName: String,index: Int) {
        
        //设置title
        //vc.title = "首页"
        //需要同时设置title和图片才能够显示
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.tag = index
        //设置选中的image
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置文字颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 10)], for: .normal)
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        vc.tabBarItem.badgeValue = "10"
        vc.tabBarItem.badgeValue = nil
        vc.tabBarItem.badgeColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)
        vc.tabBarItem.title = title
        vc.navigationItem.title = title
        //设置图片的偏移
        //vc.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
        //2.将tableVC包装到 导航控制器
        let nav = WBBaseNavController(rootViewController: vc)
        //3.将导航控制器添加到 UITabBarController对象的子视图控制器
        self.addChildViewController(nav)
    }
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        print(item.tag)
        //1.应该获取到被点击的UITabBarButton
        var index = 0
        for subView in tabBar.subviews {
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                if index == item.tag {
                    //可以获取到被点击tabbarButton
                    //遍历子视图
                    for v in subView.subviews {
                        if v.isKind(of: NSClassFromString("UIImageView")!) {
                            print("终于找到你,还好没放弃\(v)")
                            //线缩小
                            v.transform = CGAffineTransform.init(scaleX: 0.6, y: 0.6)
                            UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 6, options: [], animations: {
                                v.transform = CGAffineTransform.identity
                            }, completion: { (_) in
                                print("👌")
                            })
                        }
                    }
                }
                index += 1
            }
        }
    }
    
    
}
